const axios = require('axios')
module.exports = class extends enkel.controller.base {
  init (http) {
    super.init(http)

    this.PluginModel = this.models('inig/plugin');
    this.CategoryModel = this.models('inig/category');
    // this.PluginModel.belongsTo(this.CategoryModel, {
    //   // as: 'user',
    //   foreignKey: 'category',
    //   // targetKey: 'id',
    // })
    // this.PluginModel.belongsTo(this.CategoryModel, {
    //   foreignKey: 'category',
    //   targetKey: 'id',
    // })
    // this.CategoryModel.hasMany(this.PluginModel, {
    //   // targetKey: 'category',
    //   // foreignKey: 'id',
    //   as: 'list'
    // })

    this.CategoryModel.hasMany(this.PluginModel, {
      // as: 'list',
      targetKey: 'category',
      foreignKey: 'id',
    })
    // this.PluginModel.belongsTo(this.CategoryModel, {
    //   as: 'list',
    //   targetKey: 'id',
    //   foreignKey: 'category',
    // })

    this.Op = this.Sequelize.Op;

    this.response.setHeader('Access-Control-Allow-Origin', '*')
    this.response.setHeader('Access-Control-Allow-Headers', '*')
    this.response.setHeader('Access-Control-Allow-Methods', '*')
  }

  async add () {
    this.PluginModel.create({
      pid: 'iJ-dhYmLf7',
      category: '406c3068-6b4d-4863-b040-4be6fba837b4',
      pluginName: '剪切板',
      description: '剪切板管理',
      size: '1623984',
      version: '1.1.0',
      logo: 'http://img.dei2.com/18000000001/logo.png?Fv3h3_9GrUT8F545TSdP9UTgDvVx',
      phonenum: '18000000001',
      main: 'index.html',
      features: { "searchable": { "cmds": ["Clipboard", "剪切板", "剪贴板", "剪切板管理", "剪贴板管理"] }, "filterable": true, "singleWindow": true },
      windowOptions: { "width": 350, "height": 600 },
      author: 'BBTools',
      homepage: 'https://bbt.dei2.com',
      url: 'http://img.dei2.com/18000000001/36ff9043-8f15-43f2-ab69-af978e4d36f8.bbt?Fhx7sM8o83Q5Xf-lSJD-OdmkS5B4',
      status: 1
    })

    this.PluginModel.create({
      pid: 'bbtlovesyou',
      // category: NULL,
      pluginName: '插件中心',
      description: 'BBTools 插件管理中心',
      size: '3063659',
      version: '1.0.6',
      logo: 'http://img.dei2.com/18000000001/logo.png?FuOa9qKvZN-6JiN_7LwHHzYFjfB5',
      phonenum: '18000000001',
      main: 'index.html',
      features: { "searchable": { "cmds": ["Plugins", "插件中心", "插件管理"], "description": "BBTools 插件管理中心" }, "filterable": true },
      windowOptions: { "width": 1000, "height": 800 },
      author: 'BBTools',
      homepage: 'https://bbt.dei2.com',
      url: 'http://img.dei2.com/18000000001/bbtlovesyou.bbt?FsM7Mck_VhMGTsrX8kT_WRVsIj-T',
      status: 1
    })

    this.PluginModel.create({
      pid: 'uYbSDv4mrI',
      category: '406c3068-6b4d-4863-b040-4be6fba837b4',
      pluginName: 'json',
      description: '增强型json格式化，url参数解析',
      size: '11271141',
      version: '1.0.1',
      logo: 'http://img.dei2.com/18000000001/logo.png?Ft_DWGx14k7jM4tTRduZ3tlfFk-c',
      phonenum: '18000000001',
      main: 'index.html',
      features: { "searchable": { "cmds": ["json", "json格式化", "url参数解析"] }, "filterable": true },
      windowOptions: { "width": 1000, "height": 800 },
      author: 'BBTools',
      homepage: 'https://bbt.dei2.com',
      url: 'http://img.dei2.com/18000000001/uYbSDv4mrI.bbt?lpoP7hCINM1lDPJYLQad9AErNrfw',
      status: 1
    })

    this.PluginModel.create({
      pid: 'jVzCL7TqCq',
      category: '406c3068-6b4d-4863-b040-4be6fba837b4',
      pluginName: '二维码',
      description: '二维码工具，生成二维码，二维码解析',
      size: '2543929',
      version: '1.0.28',
      logo: 'http://img.dei2.com/18000000001/logo.png?FoysiTNMo7M2rmk7ZP02FDgz0o1w',
      phonenum: '18000000001',
      main: 'index.html',
      features: { "searchable": { "cmds": ["qrcode", "二维码"] }, "filterable": false },
      windowOptions: { "width": 1000, "height": 800, "customTraffic": true },
      author: 'BBTools',
      homepage: 'https://bbt.dei2.com',
      url: 'http://img.dei2.com/18000000001/a911ac3f-4332-4cf5-9fac-eca26b3319da.bbt?FmTmu3-ajxcVyNQpqDzTbj8Lk-ey',
      status: 1
    })

    this.PluginModel.create({
      pid: 'Nue7JVmKMn',
      category: '406c3068-6b4d-4863-b040-4be6fba837b4',
      pluginName: '内网穿透',
      description: '内网穿透工具',
      size: '1084585',
      version: '1.0.0',
      logo: 'http://img.dei2.com/18000000001/logo.png?Fhg1L9_v_teakkU4W9GMgiPrvkos',
      phonenum: '18000000001',
      main: 'index.html',
      features: { "searchable": { "cmds": ["Intranet", "内网穿透"] }, "filterable": true, "singleWindow": true },
      windowOptions: { "width": 800, "height": 600 },
      author: 'BBTools',
      homepage: 'https://bbt.dei2.com',
      url: 'http://img.dei2.com/18000000001/ffe89ad6-3b08-49a9-9982-3c3ea2fc987e.bbt?FsH6uXWRGsywjuj_qjqy3gJpZfFP',
      status: 1
    })

    this.PluginModel.create({
      pid: 'qjem6tyDkd',
      category: '406c3068-6b4d-4863-b040-4be6fba837b4',
      pluginName: 'video',
      description: '电视直播',
      size: '1367945',
      version: '1.0.3',
      logo: 'http://img.dei2.com/18000000001/logo.png?Fvpxy0FdW3NjvOuKTtzAZLfs46pu&s=XW5eyrsLCdjQfbF',
      phonenum: '18000000001',
      main: 'index.html',
      features: { "searchable": { "cmds": ["video", "电视直播"] }, "filterable": true },
      windowOptions: { "width": 800, "height": 600 },
      author: 'BBTools',
      homepage: 'https://bbt.dei2.com',
      url: 'http://img.dei2.com/18000000001/d6d88c2b-89bf-4192-9698-1e15469ad876.bbt?FhhYPg2T95Sk2aYmZo34eCWmuEUv',
      status: 1
    })
    return this.json({
      message: '成功',
      status: 200
    })
  }

  async getBannersAction () {
    return this.json({
      status: 200,
      message: '成功',
      data: {
        list: [
          {
            image: 'http://static.dei2.com/bbtools/banners/banner01.jpg',
            href: 'https://bbt.dei2.com/'
          }
        ]
      }
    })
  }

  async getBasePluginConfigAction () {
    await axios({
      url: 'https://gitee.com/inig/bbtools_plugins/raw/master/resources/plugin.bbc',
      method: 'GET',
      headers: {
        'Content-Type': 'application/json'
      }
    }).then(config => {
      return this.json({
        status: 200,
        message: '成功',
        data: config.data || {}
      })
    }).catch(err => {
      return this.json({
        status: 1001,
        message: err.message || '获取失败',
        data: {}
      })
    })
  }

  async listAction () {
    let allCategories = await this.CategoryModel.findAll()
    let ps = []
    for (let i = 0; i < allCategories.length; i++) {
      ps.push(this.PluginModel.findAll({
        where: {
          category: allCategories[i].id
        },
        limit: 50,
        offset: 0,
        order: [
          ['updatedAt', 'DESC']
        ]
      }))
    }
    await Promise.all(ps).then(res => {
      allCategories.map((item, index) => {
        item['dataValues'].list = res[index] || []
        return item
      })
      return this.json({
        status: 200,
        message: '成功',
        data: {
          list: allCategories
        }
      })
    }).catch(err => {
      return this.json({
        status: 1001,
        message: err.message || '失败',
        data: {
          list: []
        }
      })
    })
  }

}
